const app = getApp()

function getInputPlaceholder(key) {
    let r = ''
    switch (key) {
        case 'plate_num':
            r = '请输入车牌'
            break
        case 'cart_type':
            r = '请输入车辆类型'
            break
        case 'cart_vin':
            r = '请输入车辆识别码'
            break
        case 'engine_number':
            r = '请输入发动机号码'
            break
    }
    return r
}

function check_licensePlate(licensePlate) {
    var re = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/;
    if (licensePlate.search(re) == -1) {
        return false;
    } else {
        return true;
    }
}
Page({

    /**
     * 页面的初始数据
     */
    data: {
        propArray: [],
        carTypeArray: [],
        propIndex: 0,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData(options)
        this.loadData()
    },
    loadData() {
        Promise.all([
            app.http.post('rest/dictionary/getProvinces.do'),
            app.http.post('rest/dictionary/getCarTypes.do'),
        ]).then(res => {
			let currentTypeIndex = 0
			for (let i = 0; i < res[1].obj.length; i++) {
				if (res[1].obj[i].label == '小型汽车') {
					currentTypeIndex = i
					break
				}
			}
			this.setData({
				propArray: res[0].obj,
				carTypeArray: res[1].obj,
				currentType: currentTypeIndex,
				carTypeValue:res[1].obj[currentTypeIndex].value
			})
			this.loadCart()
			wx.hideLoading()
        })
    },
    loadCart() {
        let {
            propIndex = 0, currentType = 1, propArray = [], carTypeArray = []
        } = this.data
        if (!this.data.cart_id) return
        wx.showLoading({
            title: '加载中...',
        })
        app.http.post('rest/car/queryById.do', {
            data: this.data.cart_id
        }).then(res => {
			let reg = /([\u4e00-\u9fa5]{1})[A-Za-z0-9]{6}/
			let a = res.obj.carNo.match(reg)
			if (a) {
				for (let i = 0; i < propArray.length; i++) {
					if (propArray[i].value == a[1]) {
						propIndex = i
						break
					}
				}
			}
            for (let i = 0; i < carTypeArray.length; i++) {
                if (carTypeArray.value == res.obj.type) {
                    currentType = i
                    break
                }
            }
            this.setData({
                propIndex,
                plate_num: res.obj.carNo.substring(1, res.obj.carNo.length),
                currentType,
                engine_number: res.obj.engineNo,
                cart_vin: res.obj.frameNo
            })
            wx.hideLoading()
        })
    },
    //同步方法
    //let base64 = wx.getFileSystemManager().readFileSync(res.tempFilePaths[0], 'base64')
    scan() {
        let {
            propIndex = 0, propArray = [], carTypeArray = []
        } = this.data
        wx.chooseImage({
            count: 1,
            success: res => {
                wx.getFileSystemManager().readFile({
                    filePath: res.tempFilePaths[0],
                    encoding: 'base64',
                    success: res => {
                        let imageUrl = 'data:image/png;base64,' + res.data
                        console.log(imageUrl)
                        wx.showLoading({
                            title: '识别中...',
                        })
                        app.http.post('rest/orc/vehicle.do', {
                            data: imageUrl
                        }).then(res => {
							let reg = /([\u4e00-\u9fa5]{1})[A-Za-z0-9]{6}/
							let a = res.obj.carNo.match(reg)
							console.log(res.obj.carNo)
							if (a) {
								for (let i = 0; i < propArray.length; i++) {
									if (propArray[i].value == a[1]) {
										propIndex = i
										console.log(propIndex, 'propindex')
										break
									}
								}
							}
                            this.setData({
                                propIndex,
                                plate_num: res.obj.carNo.substring(1, res.obj.carNo.length),
                                engine_number: res.obj.engineNo,
                                cart_vin: res.obj.frameNo
                            })
                            wx.hideLoading()
                        })
                    }
                })
            }
        })
    },
    bindCartNo(evt) {
        let value = evt.detail.value.toUpperCase()
        this.setData({
            plate_num: value
        })
    },
    bindPickerChange(evt) {
        let index = evt.detail.value
        this.setData({
            currentType: index,
            carTypeValue: this.data.carTypeArray[index].value
        })
    },
    submit(evt) {
        let obj = evt.detail.value
        for (var key in obj) {
            if (!obj[key]) {
                wx.showToast({
                    icon: 'none',
                    title: getInputPlaceholder(key),
                })
                return
            }
        }
        let carNo = this.data.propArray[this.data.propIndex].value + obj.plate_num
        if (check_licensePlate(carNo) == false) {
            wx.showToast({
                icon: 'none',
                title: '车牌号不正确',
            })
            return
        }
        wx.showLoading({
            title: '请稍等...',
        })
        let data = {
            carNo: carNo,
            type: this.data.carTypeValue,
            frameNo: obj.cart_vin,
            engineNo: obj.engine_number
        }
        app.http.post('rest/car/save.do', {
            data
        }).then(res => {
            this.setData({
                paying: true
            })
            wx.hideLoading()
            wx.showToast({
                icon: 'none',
                title: '添加成功',
            })
            wx.navigateBack({})

        }).catch(err => {
            this.setData({
                paying: false
            })
        })
    },
    //删除绑定车辆
    deleteCar(evt) {
        wx.showModal({
            title: '温馨提示',
            content: '您确定要删除这个绑定的车辆吗?',
            confirmColor: '#4DB4FF',
            success: res => {
                if (res.confirm) {
                    wx.showLoading({
                        title: '加载中...',
                    })
                    app.http.post('rest/car/delete.do', {
                        data: this.data.cart_id
                    }).then(res => {
                        wx.hideLoading()
                        wx.showToast({
                            icon:'none',
                            title: '删除车辆成功',
                        })
						app.getPrevPage().setData({
							page: 1,
							total: 2,
							loading: false,
							cartList: []
						})
						app.getPrevPage().loadData()
                        wx.navigateBack({})
                    })
                }
            }
        })
    },
})